//
//  ViewController.m
//  综合练习-1
//
//  Created by student on 16/2/26.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property(nonatomic,assign) int x;
@property(nonatomic,assign) int y;
@property (nonatomic,assign) int count;
@property(nonatomic,strong) NSArray *shops;
@property (weak, nonatomic) IBOutlet UIView *shopsView;
@property(nonatomic,strong) NSMutableArray *views;
@end

@implementation ViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.count = 0;
        self.x = 0;
        self.y = 0;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *path=[[NSBundle mainBundle]pathForResource:@"shops" ofType:@"plist"];
    self.shops=[NSArray arrayWithContentsOfFile:path];
    self.views=[[NSMutableArray alloc]init];
    
}
- (void)addviewImage:(NSString *)i andText:(NSString *)t andX:(int)x andY:(int)y
{
    UIView *Aview = [[UIView alloc] initWithFrame:CGRectMake(x, y, 120, 120)];
    Aview.backgroundColor=[UIColor whiteColor];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];
    image.image = [UIImage imageNamed:i];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 100, 20)];
    label.text = t;
    [Aview addSubview:image];
    [Aview addSubview:label];
    [self.views addObject:Aview];
    [self.shopsView addSubview:Aview];
}
- (IBAction)addBtnPressed:(id)sender
{
    if(self.count<6)
    {
        NSDictionary *info=[[NSDictionary alloc]initWithDictionary:self.shops[self.count]];
        
        NSString *name=[[NSString alloc]initWithFormat:@"%@",info[@"name"] ];
        NSString *icon=[[NSString alloc]initWithFormat:@"%@",info[@"icon"] ];
        [self addviewImage:icon andText:name andX:self.x andY:self.y];
        self.count++;
        self.x+=130;
        
        switch (self.count) {
            case 3:
                self.x=0;
                self.y=150;
                break;
            default:
                break;
        }
    }
    else if (self.count==6)
    {
        UIAlertView *art=[[UIAlertView alloc]initWithTitle:@"提示" message:@"无法添加图片" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [art show];
        self.btn1.enabled=YES;
    }
}

- (IBAction)removeBtnPressed:(id)sender
{
    if(self.count>0)
    {
        self.count--;
        [self.views[self.count] removeFromSuperview];
        [self.views removeObject:self.views[self.count]];
        self.x-=130;
        switch (self.count) {
            case 2:
                self.x=260;
                self.y=0;
                break;
            default:
                break;
        }
        
    }
    else
    {
        UIAlertView *art=[[UIAlertView alloc]initWithTitle:@"提示" message:@"无法删除图片" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [art show];
        self.x=0;
        self.y=0;
        self.btn2.enabled=YES;
    }

}

@end
